package redisutil

import (
	"context"
	"fmt"

	"gitee.com/night-tc/gobige/common"
	"gitee.com/night-tc/gobige/global"
	"gitee.com/night-tc/gobige/redislib"
)

/*
账号数据
*/
type LoginUser struct {
	key string
	//获取redis操作的方法
	getRedisF func() *redislib.RedisHandleModel
}

func NewLoginUser(uid uint64) *LoginUser {
	return &LoginUser{
		key:       fmt.Sprintf("LoginUser:%d", uid),
		getRedisF: global.GetRedisAccess(global.RedisName_Login).GetConn,
	}
}

const (
	LoginUser_Key_UserID    = "userid"
	LoginUser_Key_OpenID    = "openid"
	LoginUser_Key_ChannelID = "channelid"

	LoginUser_Key_DeviceID = "deviceid"
	LoginUser_Key_IsAdult  = "isadult"
	LoginUser_Key_Age      = "age"
)

func (this *LoginUser) SaveInfo(v map[string]any) error {

	rd := this.getRedisF()

	defer rd.Expire(context.Background(), this.key, global.Expire3Day)
	return rd.HMSet(context.Background(), this.key, v).Err()
}

func (this *LoginUser) GetInfo() (common.GMap, error) {

	rd := this.getRedisF()

	return rd.HGetAll(context.Background(), this.key).Result()
}

// 刷新过期时间
func (this *LoginUser) Expire() {
	c := this.getRedisF()
	c.Expire(c.Ctx, this.key, global.Expire3Day)
}
